<?php

/**
 * XHTML_Head, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Head class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_I18n_Element.php";
include_once "XHTML_Title.php";
include_once "XHTML_Base.php";
include_once "XHTML_Meta.php";
include_once "XHTML_Link.php";
include_once "XHTML_Style.php";
include_once "XHTML_Script.php";

/**
 * The XHTML_Head class is a handler for the head xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Head extends XHTML_I18n_Element {
    
    // xhtml attributes
    
    /**
     * Unique indentifier for the id xhtml-head-tag.
     *
     * @var string
     */
    protected $id;
    
    /**
     * A space separated list of URL's that contains meta data information 
     * about the page...
     *
     * @var string
     */
    protected $profile;
    
    // xhtml elements
    
    /**
     * Required XHTML_Title element.
     * 
     * @var XHTML_Title
     */
    protected $title;
    
    /**
     * Optional XHTML_Base element.
     *
     * @var XHTML_Base
     */
    protected $base;
    
    /**
     * Optional XHTML_Script array.
     * 
     * @var array
     */
    protected $scripts;
    
    /**
     * Optional XHTML_Style array.
     * 
     * @var array
     */
    protected $styles;
    
    /**
     * Optional XHTML_Meta array.
     *
     * @var array
     */
    protected $metas;
    
    /**
     * Optional XHTML_Link array.
     *
     * @var array
     */
    protected $links;
    
    /**
     * Optional XHTML_Object array.
     *
     * @var array
     */
    protected $objects;
    
    
    // Methods
    
    /**
     * Constructs a XTHML_Head object.
     *
     * @param XHTML_Title $title
     */
    function __construct(XHTML_Title $title)
    {
        $this->title = $title;
        settype($this->scripts, "array");
        settype($this->styles, "array");
        settype($this->metas, "array");
        settype($this->links, "array");
        settype($this->objects, "array");
        $metaChar = new XHTML_Meta("text/html; charset=iso-8859-1");
        $metaChar->setHttpEquiv("Content-Type");
        $this->addMeta($metaChar);
    }
    
    /**
     * Sets the unique identifier.
     *
     * @param string $id
     */
    function setId($id)
    {
        $this->id = $id;
    }
    
    /**
     * gets the unique identifier.
     *
     * @return string
     */
    function getId()
    {
        return $this->id;
    }
    
    /**
     * Sets a space separated list of URL's that contains meta data information
     *  about the page.
     *
     * @param string $profile
     */
    function setProfile($profile)
    {
        $this->profile = $profile;
    }
    
    /**
     * Gets a space separated list of URL's that contains meta data information
     *  about the page...
     * 
     * @return string
     */
    function getProfile()
    {
        return $this->profile;
    }
    
    /**
     * Sets the title element for this object.
     * 
     * @param XHTML_Title $title
     */
    function setTitle(XHTML_Title $title)
    {
        $this->title = $title;
    }
    
    /**
     * Gets the title element that has been set for this object.
     * 
     * @return XHTML_Title
     */
    function getTitle()
    {
        return $this->title;
    }
    
    /**
     * Sets the base element for this object.
     * 
     * @param XHTML_Base $base
     */
    function setBase(XHTML_Base $base)
    {
        $this->base = $base;
    }
    
    /**
     * Gets the base element that has been set for this object.
     * 
     * @return XHTML_Base
     */
    function getBase()
    {
        return $this->base;
    }
    
    /**
     * Sets the XHTML_Script array for this object.
     * 
     * @param array $scripts
     */
    function setScripts($scripts)
    {
        $this->scripts = $scripts;
    }
    
    /**
     * Gets the XHTML_Script array that has been set for this object.
     * 
     * @return array
     */
    function getScripts()
    {
        return $this->scripts;
    }
    
    /**
     * Adds a XHTML_Script object to the XHTML_Script array contained in this
     * object.
     * 
     * @param XTHML_Script $script
     */
    function addScript(XHTML_Script $script)
    {
    	$this->scripts[] = $script;
    }
    
    /**
     * Removes a XHTML_Script object from the XHML_Script array contained in 
     * this object
     *
     * @param XHTML_Script $script
     */
    function removeScript(XHTML_Script $script)
    {
        if (($res = array_search($script, $this->scripts)) !== false) {
            unset($this->scripts[$res]);
        }
    }
    
    /**
     * Sets the XHTML_Style array for this object.
     * 
     * @param array $scripts
     */
    function setStyles($styles)
    {
        $this->styles = $styles;
    }
    
    /**
     * Gets the XHTML_Style array that has been set for this object.
     * 
     * @return array
     */
    function getStyles()
    {
        return $this->styles;
    }
    
    /**
     * Adds a XHTML_Style object to the XHTML_Style array contained in this
     * object.
     * 
     * @param XTHML_Style $style
     */
    function addStyle(XHTML_Style $style)
    {
        array_push($this->styles, $style);
    }
    
    /**
     * Removes a XHTML_Style object from the XHML_Style array contained in 
     * this object
     *
     * @param XHTML_Style $style
     */
    function removeStyle(XHTML_Style $style)
    {
        if (($res = array_search($style, $this->styles)) !== false) {
            unset($this->styles[$res]);
        }
    }
    
    /**
     * Sets the XHTML_Meta array for this object.
     * 
     * @param array $scripts
     */
    function setMetas($metas)
    {
        $this->metas = $metas;
    }
    
    /**
     * Gets the XHTML_Meta array that has been set for this object.
     * 
     * @return array
     */
    function getMetas()
    {
        return $this->metas;
    }
    
    /**
     * Adds a XHTML_Meta object to the XHTML_Meta array contained in this
     * object.
     * 
     * @param XTHML_Meta $meta
     */
    function addMeta(XHTML_Meta $meta)
    {
        array_push($this->metas, $meta);
    }
    
    /**
     * Removes a XHTML_Meta object from the XHML_Meta array contained in 
     * this object
     *
     * @param XHTML_Meta $meta
     */
    function removeMeta(XHTML_Meta $meta)
    {
        if (($res = array_search($meta, $this->metas)) !== false) {
            unset($this->metas[$res]);
        }
    }
    
    /**
     * Sets the XHTML_Link array for this object.
     * 
     * @param array $scripts
     */
    function setLinks($links)
    {
        $this->links = $links;
    }
    
    /**
     * Gets the XHTML_Link array that has been set for this object.
     * 
     * @return array
     */
    function getLinks()
    {
        return $this->links;
    }
    
    /**
     * Adds a XHTML_Link object to the XHTML_Link array contained in this
     * object.
     * 
     * @param XTHML_Link $link
     */
    function addLink(XHTML_Link $link)
    {
        array_push($this->links, $link);
    }
    
    /**
     * Removes a XHTML_Link object from the XHML_Link array contained in 
     * this object
     *
     * @param XHTML_Link $link
     */
    function removeLink(XHTML_Link $link)
    {
        if (($res = array_search($link, $this->links)) !== false) {
            unset($this->links[$res]);
        }
    }
    
    // Overriding the toString method for a proper implementation.
    
    /**
     * Returns the string representation for echoing the class atributes.
     *
     * @return string
     */
    function toString()
    {
        $auxString = "<head".parent::toString();
        
        /*
         * Additions of the head own attributes to the return string.
         */
        if (isset($this->id)) {
            $auxString = $auxString."id=\"".$this->id."\" ";
        }
        
        if (isset($this->profile)) {
            $auxString = $auxString."profile=\"".$this->profile."\" ";
        }
        
        $auxString = $auxString.">\n";
        
        /*
         * Additions of the inner elements of the head element to the return 
         * string.
         */
        $auxString = $auxString.$this->title->toString();
        
        if (count($this->styles) > 0) {
            foreach ($this->styles as $style) {
                $auxString = $auxString.$style->toString();
            }
        }
        
        if (count($this->metas) > 0) {
            foreach ($this->metas as $meta) {
                $auxString = $auxString.$meta->toString();
            }    
        }
        
        if (count($this->metas) > 0) {
            foreach ($this->links as $link) {
                $auxString = $auxString.$link->toString();
            }
        }
        
        if (isset($this->base)) {
            $auxString = $auxString.$this->base->toString();
        }
        
        if (count($this->scripts) > 0) {
            foreach ($this->scripts as $script) {
                $auxString = $auxString.$script->toString();
            }
        }
        
        $auxString = $auxString."</head>\n";
        
        return $auxString;
    }
    
}

?>